/* @formatter:off
 * Copyright (c) 2011 Lucas <lucas03093@gmail.com>
 * 
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* @formatter:on */

package nu.korkad.daytracker.info;

import nu.korkad.daytracker.R;
import android.content.Context;
import android.text.format.DateFormat;

public class Dates
{
    /**
     * Formats a timestamp for the archive row layout.
     * 
     * @param timestamp
     *            long
     * @param context
     * @return formatted date.
     */
    public static String getArchiveDate(long timestamp, Context context)
    {
        if (timestamp == 0)
            return "?";
        else
            return DateFormat.getTimeFormat(context).format(timestamp * 1000) + " "
                    + DateFormat.getDateFormat(context).format(timestamp * 1000);
    }
    
    /**
     * Returns the duration in human readable format.
     * 
     * @param time1
     *            Long
     * @param time2
     *            Long
     * @param context
     *            Context
     * @return "x hours x minutes" or "x seconds"
     */
    public static String getDuration(long time1, long time2, Context context)
    {
        long diff = time2 - time1;
        
        long secondInMillis = 1000;
        long minuteInMillis = secondInMillis * 60;
        long hourInMillis = minuteInMillis * 60;
        long dayInMillis = hourInMillis * 60;
        long yearInMillis = dayInMillis * 365;
        
        @SuppressWarnings("unused")
        long elapsedYear = diff / yearInMillis;
        diff = diff % yearInMillis;
        @SuppressWarnings("unused")
        long elapsedDays = diff / dayInMillis;
        diff = diff % dayInMillis;
        long elapsedHours = diff / hourInMillis;
        diff = diff % hourInMillis;
        long elapsedMinutes = diff / minuteInMillis;
        diff = diff % minuteInMillis;
        long elapsedSeconds = diff / secondInMillis;
        
        // Convert negative to positive
        /*if (elapsedYear < 0)
            elapsedYear = -elapsedYear;
        if (elapsedDays < 0)
            elapsedDays = -elapsedDays;
        if (elapsedHours < 0)
            elapsedHours = -elapsedHours;
        if (elapsedMinutes < 0)
            elapsedMinutes = -elapsedMinutes;
        if (elapsedSeconds < 0)
            elapsedSeconds = -elapsedSeconds;*/

        // Show different format depending on time
        if (elapsedHours == 0 && elapsedMinutes == 0)
            return elapsedSeconds + " " + context.getString(R.string.layout_archive_row_seconds);
        else
            return elapsedHours + " " + context.getString(R.string.layout_archive_row_hours) + " " + elapsedMinutes + " "
                    + context.getString(R.string.layout_archive_row_minutes);
    }
}
